package com.xrit;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class test {
    public static void main(String[] args) throws Exception{
        //创建配置类
        Configuration configuration = new Configuration(Configuration.getVersion());
        //设置模板所在的目录
        configuration.setDirectoryForTemplateLoading(new File("D:\\ftl"));
        //设置字符集
        configuration.setDefaultEncoding("UTF-8");

        //加载模板
        Template template = configuration.getTemplate("test.ftl");
        //创建数据模型
        Map map=new HashMap();
        map.put("name", "老六");
        map.put("message", "欢迎来到极乐世界！");
        map.put("linkman","老六");
        map.put("success",false);

        ArrayList list = new ArrayList();
        HashMap<String,Object> map1 = new HashMap<>();
        map1.put("name","PS5");
        map1.put("price",2599);

        HashMap<String, Object> map2 = new HashMap<>();
        map2.put("name","iphone");
        map2.put("price",8888);
        list.add(map1);
        list.add(map2);
        map.put("goodsList",list);

        HashMap<String, String> info = new HashMap<>();
        info.put("mobile","10086");
        info.put("address","中国移动");
        map.put("info",info);
        //创建输出文件路径
        FileWriter writer = new FileWriter(("D:\\IdeaProjects\\A122_S3\\sunnyday_parent\\freemark-demo\\src\\main\\webapp\\pages\\test.html"));
        //数据填充到模板
        template.process(map,writer);
        //释放流
        writer.close();
    }
}
